<?php

namespace addons\shopro\model;

use think\Model;
use addons\shopro\exception\Exception;
use think\Db;
use app\admin\model\share\ShareUser;


/**
 * 钱包
 */
class Feedback extends Model
{

    // 表名,不含前缀
    protected $name = 'shopro_feedback';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    protected $hidden = ['deletetime'];


    // 追加属性
    protected $append = [
        'type_name', 'status_name'
    ];

    public static $typeAll = [
        'product' => ['code' => 'product', 'name' =>'产品功能问题反馈'],
        'feedback' => ['code' => 'feedback', 'name' => '建议及意见反馈'],
        'complaint' => ['code' => 'complaint', 'name' => '投诉客服其他问题'],
    ];

    public static function add($params)
    {
        $user = User::info();
        
        extract($params);

        $data = [
            "user_id" => $user->id,
            "type" => $type,
            "content" => $content,
            "phone" => $phone,
            'status' => 0
        ];
        if (!empty($params['images'])) {
            $data['images'] = implode(',', $images);
        }
        $self = self::create( $data );

        return $self;
    }

    public static function edit($params)
    {
        $user = User::info();
        
        extract($params);
        
        $self = self::where(["id" => $id])->update([
            "handle_user_id" => $user->id,
            "append_remark" => $append_remark,
            'append_time' => time()
        ]);

        return $self;
    }

    public static function my_list($params)
    {
        $user = User::info();
        
        extract($params);
        $per_page = $per_page ?? 10;

        $self = self::with(['user' => function ($query) {
            $query->field('id, nickname, avatar');
        },'classify' => function ($query) {
            $query->field('id, name');
        },'oper'=> function ($query) {
            $query->field('id, nickname');
        },'handleuser'=> function ($query) {
            $query->field('id, nickname');
        }])->where([
            "user_id" => $user->id,
            'status' => $status
        ])->select();

        return $self;
    }

    public static function list($params)
    {
        extract($params);
        $per_page = $per_page ?? 10;
        
        $user = User::info();
        $share = ShareUser::where(["user_id" => $user->id])->find();
        if (empty($share)) {
            return '';
        }

        $flags_list = Db::name("share_user_flags")->where(['user_flags' => $share['user_flags']])->select();
        $user_id_arr = array_column($flags_list, 'user_id');
        
        $where = array('status' => $status);
        if (!empty($type)) {
            $where['type'] = $type;
        }
        if (!empty($nickname)) {
            $temp_user = User::where(["nickname" => ['like','%'.$nickname.'%']])->select();
            $user_arr = array_column($temp_user, 'id');
            if (!empty($temp_user)) {
                $where['user_id'] = ['in',$user_arr];
            }
        }

        if (!empty($createtime)) {
            $s_time = strtotime($createtime.' 00:00:00');
            $e_time =  strtotime($createtime.' 23:59:59');
            $where['createtime'] = ['between time',[$s_time,$e_time]];
        }

        //if ($share['is_union'] == 0 && $share['is_leader'] == 0) {
            $user_id_arr[] = $user->id;
        //}

        $self = self::with(['user' => function ($query) {
            $query->field('id, nickname, avatar');
        },'classify' => function ($query) {
            $query->field('id, name');
        },'oper'=> function ($query) {
            $query->field('id, nickname');
        },'handleuser'=> function ($query) {
            $query->field('id, nickname');
        }])->where($where)->where('user_id', 'in', $user_id_arr)->paginate($per_page)->toArray();

        return $self;
    }

    public static function detail($params)
    {
        extract($params);
        $self = self::with(['user' => function ($query) {
            $query->field('id, nickname, avatar');
        },'classify' => function ($query) {
            $query->field('id, name');
        },'oper'=> function ($query) {
            $query->field('id, nickname');
        },'handleuser'=> function ($query) {
            $query->field('id, nickname');
        }])->where([
            "id" => $id
        ])->find();

        return $self;
    }


    public static function getTypeName($type) {
        return isset(self::$typeAll[$type]) ? self::$typeAll[$type]['name'] : '';
    }


    public function getTypeNameAttr($value, $data) {
        return self::getTypeName($data['type']);
    }

    public function getStatusNameAttr($value, $data) {
        return $data['status'] == 1 ? '已处理' : '未处理';
    }

    public function user()
    {
        return $this->belongsTo(\addons\shopro\model\User::class, 'user_id', 'id');
    }

    public function classify()
    {
        return $this->belongsTo(\app\admin\model\FeedbackClassify::class, 'type', 'id');
    }

    public function oper()
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'oper_id', 'id');
    }

    public function handleuser()
    {
        return $this->belongsTo(\addons\shopro\model\User::class, 'handle_user_id', 'id');
    }


    
}
